<template>
  <el-dialog
    :title="!dataForm.productId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="商户id" prop="commercialTenantId">
      <el-input v-model="dataForm.commercialTenantId" placeholder="商户id"></el-input>
    </el-form-item>
    <el-form-item label="商品名" prop="productName">
      <el-input v-model="dataForm.productName" placeholder="商品名"></el-input>
    </el-form-item>
    <el-form-item label="保质期（月）" prop="productExpirati">
      <el-input v-model="dataForm.productExpirati" placeholder="保质期（月）"></el-input>
    </el-form-item>
    <el-form-item label="产地" prop="productFactory">
      <el-input v-model="dataForm.productFactory" placeholder="产地"></el-input>
    </el-form-item>
    <el-form-item label="价格" prop="productPrice">
      <el-input v-model="dataForm.productPrice" placeholder="价格"></el-input>
    </el-form-item>
    <el-form-item label="促销价" prop="productPromoti">
      <el-input v-model="dataForm.productPromoti" placeholder="促销价"></el-input>
    </el-form-item>
    <el-form-item label="库存" prop="productStock">
      <el-input v-model="dataForm.productStock" placeholder="库存"></el-input>
    </el-form-item>
    <el-form-item label="商品图片" prop="productImg">
      <el-input v-model="dataForm.productImg" placeholder="商品图片"></el-input>
    </el-form-item>
    <el-form-item label="商品种类" prop="categoryId">
      <el-input v-model="dataForm.categoryId" placeholder="商品种类"></el-input>
    </el-form-item>
    <el-form-item label="商品描述" prop="productDesc">
      <el-input v-model="dataForm.productDesc" placeholder="商品描述"></el-input>
    </el-form-item>
    <el-form-item label="是否推荐" prop="productRecommend">
      <el-input v-model="dataForm.productRecommend" placeholder="是否推荐"></el-input>
    </el-form-item>
    <el-form-item label="是否上架" prop="productRacking">
      <el-input v-model="dataForm.productRacking" placeholder="是否上架"></el-input>
    </el-form-item>

    <el-form-item label="创建人" prop="createBy">
      <el-input v-model="dataForm.createBy" placeholder="创建人"></el-input>
    </el-form-item>

    <el-form-item label="修改人" prop="updateBy">
      <el-input v-model="dataForm.updateBy" placeholder="修改人"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          productId: 0,
          commercialTenantId: '',
          productName: '',
          productExpirati: '',
          productFactory: '',
          productPrice: '',
          productPromoti: '',
          productStock: '',
          productImg: '',
          categoryId: '',
          productDesc: '',
          productRecommend: '',
          productRacking: '',

          createBy: '',

          updateBy: ''
        },
        dataRule: {
          commercialTenantId: [
            { required: true, message: '商户id不能为空', trigger: 'blur' }
          ],
          productName: [
            { required: true, message: '商品名不能为空', trigger: 'blur' }
          ],
          productExpirati: [
            { required: true, message: '保质期（月）不能为空', trigger: 'blur' }
          ],
          productFactory: [
            { required: true, message: '产地不能为空', trigger: 'blur' }
          ],
          productPrice: [
            { required: true, message: '价格不能为空', trigger: 'blur' }
          ],
          productPromoti: [
            { required: true, message: '促销价不能为空', trigger: 'blur' }
          ],
          productStock: [
            { required: true, message: '库存不能为空', trigger: 'blur' }
          ],
          productImg: [
            { required: true, message: '商品图片不能为空', trigger: 'blur' }
          ],
          categoryId: [
            { required: true, message: '商品种类不能为空', trigger: 'blur' }
          ],
          productDesc: [
            { required: true, message: '商品描述不能为空', trigger: 'blur' }
          ],
          productRecommend: [
            { required: true, message: '是否推荐不能为空', trigger: 'blur' }
          ],
          productRacking: [
            { required: true, message: '是否上架不能为空', trigger: 'blur' }
          ],

          createBy: [
            { required: true, message: '创建人不能为空', trigger: 'blur' }
          ],

          updateBy: [
            { required: true, message: '修改人不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.productId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.productId) {
            this.$http({
              url: this.$http.adornUrl(`/shop/product/info/${this.dataForm.productId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.commercialTenantId = data.product.commercialTenantId
                this.dataForm.productName = data.product.productName
                this.dataForm.productExpirati = data.product.productExpirati
                this.dataForm.productFactory = data.product.productFactory
                this.dataForm.productPrice = data.product.productPrice
                this.dataForm.productPromoti = data.product.productPromoti
                this.dataForm.productStock = data.product.productStock
                this.dataForm.productImg = data.product.productImg
                this.dataForm.categoryId = data.product.categoryId
                this.dataForm.productDesc = data.product.productDesc
                this.dataForm.productRecommend = data.product.productRecommend
                this.dataForm.productRacking = data.product.productRacking

                this.dataForm.createBy = data.product.createBy

                this.dataForm.updateBy = data.product.updateBy
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/shop/product/${!this.dataForm.productId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'productId': this.dataForm.productId || undefined,
                'commercialTenantId': this.dataForm.commercialTenantId,
                'productName': this.dataForm.productName,
                'productExpirati': this.dataForm.productExpirati,
                'productFactory': this.dataForm.productFactory,
                'productPrice': this.dataForm.productPrice,
                'productPromoti': this.dataForm.productPromoti,
                'productStock': this.dataForm.productStock,
                'productImg': this.dataForm.productImg,
                'categoryId': this.dataForm.categoryId,
                'productDesc': this.dataForm.productDesc,
                'productRecommend': this.dataForm.productRecommend,
                'productRacking': this.dataForm.productRacking,

                'createBy': this.dataForm.createBy,

                'updateBy': this.dataForm.updateBy
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
